package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.order.OrderAccessories;
import com.zmj.sy.mom.srv.aps.bean.vo.powder.PowderOutReceiveApsReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportSprayNeedResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportWireNeedResVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface OrderAccessoriesMapper extends SyMapper<OrderAccessories> {

    @Select("delete from tb_order_accessories where order_id = #{orderId}")
    void deleteReal(@Param("orderId") Integer orderId);

    @Select("SELECT o.construct_no AS constructCode, o.mater_code AS materCode, o.mater_name AS materName, o.order_qty AS orderCount, " +
            "CONCAT(a.model,',',a.spec) AS texture, SUM(part_count) AS needWeight, MIN(awd.startTime) AS planNeedTime " +
            "FROM tb_order_accessories a LEFT JOIN tb_order o ON a.order_id = o.id " +
            "LEFT JOIN ( SELECT MIN( plan_start_date_time ) AS startTime, order_id AS id FROM tb_aps_work_detail awd WHERE awd.mater_no LIKE '%P1' GROUP BY order_id ) awd ON awd.id = o.id " +
            " ${ew.customSqlSegment} " +
            "GROUP BY o.construct_no, o.mater_code, o.mater_name, o.order_qty, a.model, a.spec ")
    Page<ReportWireNeedResVo> nestWireNeed(Page<Object> page, @Param(Constants.WRAPPER) QueryWrapper<OrderAccessories> qw);

    @Select("SELECT o.construct_no AS constructCode, o.mater_code AS materCode, o.mater_name AS materName, o.order_qty AS orderCount, " +
            "a.mater_name AS texture, SUM(part_count) AS needWeight, MIN(awd.startTime) AS planNeedTime " +
            "FROM tb_order_accessories a LEFT JOIN tb_order o ON a.order_id = o.id " +
            "LEFT JOIN ( SELECT MIN( plan_start_date_time ) AS startTime, order_id AS id FROM tb_aps_work_detail awd WHERE awd.mater_no LIKE '%P1' GROUP BY order_id ) awd ON awd.id = o.id " +
            " ${ew.customSqlSegment} " +
            "GROUP BY o.construct_no, o.mater_code, o.mater_name, o.order_qty, a.mater_name ")
    Page<ReportSprayNeedResVo> nestSprayNeed(Page<Object> page, @Param(Constants.WRAPPER) QueryWrapper<OrderAccessories> qw);

    @Select("SELECT o.construct_no AS constructCode, ob.work_order_code AS workOrderCode, ob.mater_name AS compName, ob.mater_code AS plmId, " +
            "o.order_qty AS requireCount, oa.mater_name AS powderName, oa.mater_code AS materCode, oa.part_count AS sumQuota " +
            "FROM tb_order_accessories oa LEFT JOIN tb_order o ON oa.order_id = o.id LEFT JOIN tb_order_bom ob ON oa.bom_id = ob.id " +
            " ${ew.customSqlSegment} ")
    List<PowderOutReceiveApsReqVo> powderOutReceiveAps(@Param(Constants.WRAPPER) QueryWrapper<OrderAccessories> qw);

}
